package com.alili_tv.system.controller;

import com.alili_tv.system.common.BaseContext;
import com.alili_tv.system.common.LoginRequired;
import com.alili_tv.system.common.R;
import com.alili_tv.system.entity.Message;
import com.alili_tv.system.entity.User;
import com.alili_tv.system.service.MessageService;
import com.alili_tv.system.service.UserService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/message", produces = "application/json")
public class MessageController {

    @Resource
    private MessageService messageService;

    @Resource
    private UserService userService;

    /**
     * 获取消息
     * 消息来源
     * 点赞，评论
     * @return
     */
    @LoginRequired
    @GetMapping("/getMessage/{pageIndex}/{pageSize}")
    public R<Map<String, List<Message>>> getMessage(@PathVariable Integer pageIndex, @PathVariable Integer pageSize) {
        List<Message> newComment = messageService.getComment(BaseContext.getCurrentId(), pageIndex, pageSize);
        List<Message> newLike = messageService.getNewLikes(BaseContext.getCurrentId(), pageIndex, pageSize);
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .set(User::getMessage, LocalDateTime.now())
                .eq(User::getId, BaseContext.getCurrentId());
        userService.update(updateWrapper);
        Map<String, List<Message>> map = new HashMap<>() {{
            put("reply", newComment);
            put("like", newLike);
        }};
        return R.success(map);
    }

    /**
     * 显示小红点功能：用户表有两个字段
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @LoginRequired
    @GetMapping("/getReply/{pageIndex}/{pageSize}")
    public R<List<Message>> getReply(@PathVariable Integer pageIndex, @PathVariable Integer pageSize) {
        List<Message> newComment = messageService.getComment(BaseContext.getCurrentId(), pageIndex, pageSize);
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .set(User::getMessage, LocalDateTime.now())
                .eq(User::getId, BaseContext.getCurrentId());
        userService.update(updateWrapper);
        return R.success(newComment);
    }

    @LoginRequired
    @GetMapping("/getLikes/{pageIndex}/{pageSize}")
    public R<List<Message>> getLikes(@PathVariable Integer pageIndex, @PathVariable Integer pageSize) {
        List<Message> newLike = messageService.getNewLikes(BaseContext.getCurrentId(), pageIndex, pageSize);
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .set(User::getMessage, LocalDateTime.now())
                .eq(User::getId, BaseContext.getCurrentId());
        userService.update(updateWrapper);
        return R.success(newLike);
    }
}
